package com.xbongbong.parent.util;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.AddressEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.business.ClueEnum;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  为规则引擎运行提供处理的帮助类
 * @author zheng.li
 **/
public class RuleEngineUtil {


    /**
     * 将表单实体内的系统字段放入data内
     * @param entityList 表单实体列表
     */
    public static void formSystemData (List<? extends PaasFormDataEntityExt> entityList) {
        if (entityList == null) {
            return;
        }
        for (PaasFormDataEntityExt entityExt : entityList) {
            formSystemData(entityExt);
        }

    }


    /**
     * 将表单实体内的系统字段放入data内
     * @param entityExt 表单实体
     */
    public static void formSystemData (PaasFormDataEntityExt entityExt) {
        JSONObject data = entityExt.getData();
        // 系统字段的处理
        data.put(FieldTypeEnum.CREATORID.getAlias(),entityExt.getCreatorId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(),entityExt.getSerialNo());
        data.put(FieldTypeEnum.UPDATETIME.getAlias(),entityExt.getUpdateTime());
        data.put(FieldTypeEnum.ADDTIME.getAlias(),entityExt.getAddTime());
        data.put(FieldTypeEnum.DATAID.getAlias(),entityExt.getId());
        data.put(StringConstant.CORPID,entityExt.getCorpid());
        data.put(StringConstant.FORM_ID,entityExt.getFormId());
        // 省市区的处理
        JSONObject addressJson = entityExt.getData().getJSONObject(ClueEnum.ADDRESS.getAttr());
        if (addressJson != null) {
            List<String> addressAlias = AddressEnum.getAliasList();
            for (String alias : addressAlias) {
                data.put(alias,addressJson.getString(alias));
            }
        }
    }


}
